package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.DlDao;
import com.aaa.qy127.entity.Dl;
import com.aaa.qy127.entity.Staff;
import com.aaa.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Repository("DlDaoImpl")
public class DlDaoImpl implements DlDao {

    @Override
    public Dl getphoneAndpassword(String phone, String password) {
        String sql="select userid,phone,password,name,cardID,address,status from t_user where phone = ? and password = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, phone, password);
        if (maps.size()>0){
            Map<String, Object> map = maps.get(0);
            Dl dl = new Dl((Integer) map.get("userid"), (String) map.get("phone"), (String) map.get("password"), (String) map.get("name"), (String) map.get("cardID"), (String) map.get("address"), (Integer) map.get("status"));
            return dl;
        }
        return null;
    }

    @Override
    public int insertOneDl(Dl dl) {
        String sql = "insert into t_user values(null,?,?,?,?,?,0)";
        int i = JDBCUtil.update(sql,dl.getPhone(),dl.getPassword(),dl.getName(),dl.getCardID(),dl.getAddress());
        return i;
    }

    /**
     * 向 用户表中插入一条数据
     * @param dl
     * @return
     */
    @Override
    public int insertDL(Dl dl) {
        String sql = "insert into t_user values(null,?,?,?,?,?,0)";
        int i = JDBCUtil.update(sql, dl.getPhone(), dl.getPassword(), dl.getName(), dl.getCardID(), dl.getAddress());
        return i;
    }

    /**
     * 根据 传过来的 身份证号 查询
     * @param dl
     * @return
     */
    @Override
    public List<Map<String, Object>> getOneUserByCardIDAndPhone(Dl dl) {
        String sql = "select userid,phone,`password`,`name`,cardID,address from t_user where cardID = ? and phone = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, dl.getCardID(),dl.getPhone());
        if (maps.size() >0){
         return maps;
        }
        return null;
    }

    /**
     * 通过 userid  修改 该用户 信息
     * @param dl
     * @return
     */
    @Override
    public int updateOneUserByUserid(Dl dl) {
        String sql = "update t_user set address = ? where userid = ?";
        int i = JDBCUtil.update(sql, dl.getAddress(), dl.getUserid());
        return i;
    }

    /**
     * 根据该用户的 userid  查询 该用户的 信息
     * @param userid
     * @return
     */
    @Override
    public Dl getOneUserByUserid(Integer userid) {
        String sql = "select userid,phone,`password`,`name`,cardID,address from t_user where userid = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, userid);
        if (maps.size()>0){
            Map<String, Object> map = maps.get(0);
            Dl dl = new Dl((Integer) map.get("userid"), (String) map.get("phone"), (String) map.get("password"), (String) map.get("name"), (String) map.get("cardID"), (String) map.get("address"));
            return dl;
        }
        return null;
    }

    /**
     * 通过 该用户的 userid 修改 该用户的 密码
     * @param password
     * @param userid
     * @return
     */
    @Override
    public int updatePasswordByUserid(String password, String userid) {
        String sql = "update t_user set `password` = ? where userid = ?";
        int i = JDBCUtil.update(sql, password, userid);
        return i;
    }

}
